Sum of Left Leaf Nodes
Given a Binary Tree of size N. Find the sum of all the leaf nodes that are left child of their parent
of the given binary tree.
Example 1:
Input:
1
/ \
2 3
Output: 2
Example 2:
Input:
1
/ \
2 3
/ \ \
4 5 8
/ \ / \
7 2 6 9
Output: 13
Explanation:
sum = 6 + 7 = 13
Code
int leftLeavesSum(Node *root)
{
if(root==NULL)
return 0;
if((root->left)&&(root->left->left==0)&&(root->left->right==0))
return root->left->data+leftLeavesSum(root->right);
return leftLeavesSum(root->left)+leftLeavesSum(root->right);
}